package org.yn.dept.service.impl;

import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.yn.base.until.PageInfo;
import org.yn.dept.domain.Department;
import org.yn.dept.mapper.DepartmentMapper;
import org.yn.dept.service.IDepartmentService;
import org.yn.emp.domain.Employee;
import org.yn.query.DepartmentQuery;

import java.util.List;
import java.util.Objects;

@Service
public class DepartmentServiceImpl implements IDepartmentService {
    @Autowired
    DepartmentMapper departmentMapper;

    /**
     * 查找全部
     *
     * @return
     */
    @Override
    public List<Department> selectAll() {
        return departmentMapper.selectAllDept();
    }

    /**
     * 根据id删除部门
     *
     * @param id
     */
    @Override
    public void delete(Long id) {
        departmentMapper.delete(id);
    }

    /**
     * 更新
     *
     * @param department
     */
    @Override
    public void update(Department department) {
        //更新的部门数据 看看parent的id是否为空
        Long id= department.getParent().getId();
        if (Objects.isNull(id)){
            //没有父 id  那父级部门就是自己
            department.setPath("/"+department.getId());
        }else {
            //如果parent不为空  有父级 就去查这个parent部门对象
            Department parent= departmentMapper.getDeptById(id);
            // 将这个新添加进去的部门的path设置为，他父部门path+/+新部门的id
            department.setPath(parent.getPath()+"/"+department.getId());
        }
        departmentMapper.update(department);
    }

    /**
     * 新增
     *
     * @param department
     */
    @Override
    public void insert(Department department) {
        departmentMapper.insert(department);
        //处理path
        Long id= department.getParent().getId();
        //判断parent的id是否为空
        if(Objects.nonNull(id)){
            //如果parent不为空  有父级 就去查这个parent部门对象
           Department parent= departmentMapper.getDeptById(id);
            // 将这个新添加进去的部门的path设置为，他父部门path+/+新部门的id
            department.setPath(parent.getPath()+"/"+department.getId());
        }else{
            //为空没有父级，自己是一级部门，path为/自己id
            department.setPath("/"+department.getId());
        }
            //更新数据
        departmentMapper.update(department);
    }

    /**
     * 分页查询
     *
     * @param departmentQuery
     * @return
     */
    @Override
    public PageInfo<Department> queryPage(DepartmentQuery departmentQuery) {

        Integer total =departmentMapper.countTotal(departmentQuery);
        if (total==0){
            return new PageInfo<Department>();//返回一个空的
        }
        //查询分页数据
       List<Department> list= departmentMapper.queryPage(departmentQuery);

        return new PageInfo<Department>(total, list);
    }


    /**
     * 查询单个
     * @param id
     */
    @Override
    public Department selectById(Long id) {
       return departmentMapper.getDeptById(id);
    }


    @Override
    public void batchDelete(List<Long> ids) {
        departmentMapper.batchDelete(ids);
    }

    @Override
    public List<Department> getDeptTree() {
        return departmentMapper.getDeptTree();
    }
}
